package com.yanqu.road.server.manger.config;

import com.yanqu.road.entity.config.thief.ThiefInfo;
import com.yanqu.road.entity.enums.ePlayerAttrType;
import com.yanqu.road.entity.enums.eThiefStatus;
import com.yanqu.road.entity.player.UserThief;
import com.yanqu.road.logic.bussiness.config.ThiefBusiness;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.AttributeModule;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ThiefMgr extends TempMgr {

    // Map<小偷类型，小偷配置>
    private static Map<Integer, ThiefInfo> thiefInfoMap = new ConcurrentHashMap<>();

    @Override
    public boolean reloadConfig() throws Exception {
        thiefInfoMap = ThiefBusiness.getThiefInfoMap();
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static Map<Integer, ThiefInfo> getThiefInfoMap() {
        return thiefInfoMap;
    }

    public static ThiefInfo getThiefInfo(int thiefType) {
        return thiefInfoMap.get(thiefType);
    }

    /**
     * 检查小偷状态
     */
    public static eThiefStatus getThiefStatus(UserThief userThief) {

        if (userThief.getWorkBeginTime() == 0L) return eThiefStatus.NoCatch;

        if (userThief.isGetReward()) return eThiefStatus.HadGetReward;

        ThiefInfo thiefInfo = getThiefInfo(userThief.getType());
        if (thiefInfo == null) {
            getLogger().info("can not find thiefInfo! ThiefType={},ThiefUid={},UserId={}", userThief.getType(), userThief.getThiefUid(), userThief.getUserId());
            return eThiefStatus.Working;
        }
        long now = System.currentTimeMillis();
        long rewardTime = userThief.getWorkBeginTime() + thiefInfo.getWorkTime() * 60L * 1000L;
        if (rewardTime > now) {
            return eThiefStatus.Working;
        }

        return eThiefStatus.WorkingFinish;
    }

    /**
     * 是否过引导
     */
    public static boolean isPlayerHadGuide(GamePlayer player) {
        long attribute = player.getModule(AttributeModule.class).getAttribute(ePlayerAttrType.ThiefLastCheckTime);
        return attribute != 0;
    }

}
